package com.kdtech.analyse.app;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

/**
 * @链接 http://m.wallstreetcn.com/
 * @名字 华尔街见闻
 * @作者 dhy
 */
public class WallstreetcnAppAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://m.wallstreetcn.com/node/[0-9]+",
		"http://m.wallstreetcn.com/livenews/[0-9]+",
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		
		title =doc.select("h2.title").text();
		if(url.startsWith("http://m.wallstreetcn.com/livenews/")){
			String tmpTitle = doc.select("div.content").text();
			title = StringUtils.substringBetween(tmpTitle, "【", "】");
			if(StringUtils.isBlank(title) || title.length() < 10){
				title = StringUtils.substringBefore(tmpTitle, "。");
			}
		}
		
		date=DateUtils.matchDate(doc.select("div.meta").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("h2.head").text());
		}
		
		doc.select("p.hide-for-app").remove();
		content =HtmlCleaner.getContentHtml(url, doc.select("div.article-content"));
		if(StringUtils.isBlank(content)){
			content =HtmlCleaner.getContentHtml(url, doc.select("div.content"));
		}
		
		String authorStr = doc.select("span.author").text();
		author = StringUtils.substringAfter(authorStr, "文 / ");
		
		news.setTitle(StringUtils.trimSpace(title));
		news.setAuthor(author);
		news.setContent(content);
		news.setDate(date);

		return news;

	}

	

	

	
}

